package cloud.xlh.digital_power_pay.facade.inner_call.card.impl;

import cloud.xlh.digital_power_pay.client.inner_call.card.dto.req.ReversalReqDTO;
import cloud.xlh.digital_power_pay.client.inner_call.card.dto.resp.ReversalRespDTO;
import cloud.xlh.digital_power_pay.client.inner_call.card.service.DigitalPowerCardRemoteClientService;
import cloud.xlh.digital_power_pay.config.remote_call.DigitalPowerCardRemoteCall;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : xlh_osms
 * @ Author: XuLeHuang
 * @ Date: 2023/8/20 11:50
 * @ Description:
 */
@Slf4j
@Service
public class DigitalPowerCardRemoteClientServiceImpl implements DigitalPowerCardRemoteClientService {
    @Resource
    private DigitalPowerCardRemoteCall digitalPowerCardRemoteCall;

    @Override
    public ReversalRespDTO reversal(ReversalReqDTO reversalReqDTO, String callBackClassName) {
        JSONObject reqJsonObject = (JSONObject) JSON.toJSON(reversalReqDTO);
        JSONObject respJsonObject = null;
        try {
            respJsonObject = digitalPowerCardRemoteCall.reversal(reqJsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (respJsonObject == null) {
            log.error("内部调用卡系统异常，响应为空......");
        }
        return invokeCallBackClass(respJsonObject, callBackClassName);
    }

    private ReversalRespDTO invokeCallBackClass(JSONObject respJsonObject, String callBackClassName) {
        Object bean = SpringUtil.getBean(callBackClassName);
        Class<?> aClass = bean.getClass();
        JSONObject result = null;
        try {
            Method method = aClass.getMethod(callBackClassName);
            result = (JSONObject)method.invoke(respJsonObject);
        } catch (Exception e) {

        }
        return convertReversalRespDTO(result);
    }

    private ReversalRespDTO convertReversalRespDTO(JSONObject result) {
        return null;
    }

}
